package com.ruoyi.web.controller.company.uploadfile;

import java.io.File;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.company.uploadfile.domain.SysFileInfo;
import com.ruoyi.company.uploadfile.service.ISysFileInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件信息Controller
 * 
 * @author baike
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/company/uploadfile")
public class SysFileInfoController extends BaseController
{
    private String prefix = "company/uploadfile";

    @Autowired
    private ISysFileInfoService sysFileInfoService;

    @RequiresPermissions("company:uploadfile:view")
    @GetMapping()
    public String uploadfile()
    {
        return prefix + "/uploadfile";
    }

    @RequiresPermissions("company:uploadfile:uploadList")
    @GetMapping("/{id}")
    public String uploadfilelb(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("relationId", id);
        return prefix + "/uploadfile";
    }

    /**
     * 查询文件信息列表
     */
    @RequiresPermissions("company:uploadfile:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFileInfo sysFileInfo)
    {
        startPage();
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        return getDataTable(list);
    }

    /**
     * 查询文件信息列表弹窗
     */
    //@RequiresPermissions("company:uploadfile:list")
    @PostMapping("/list/{id}")
    @ResponseBody
    public TableDataInfo uploadlist(@PathVariable("id") String id,SysFileInfo sysFileInfo)
    {
        startPage();
        sysFileInfo.setRelationId(id);
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件信息列表
     */
    @RequiresPermissions("company:uploadfile:export")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysFileInfo sysFileInfo)
    {
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        ExcelUtil<SysFileInfo> util = new ExcelUtil<SysFileInfo>(SysFileInfo.class);
        return util.exportExcel(list, "uploadfile");
    }

    /**
     * 新增文件信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件信息
     */
    @RequiresPermissions("company:uploadfile:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysFileInfo sysFileInfo)
    {
    	sysFileInfo.setFileId(IdUtils.simpleUUID());
        return toAjax(sysFileInfoService.insertSysFileInfo(sysFileInfo));
    }

    /**
     * 修改文件信息
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") String fileId, ModelMap mmap)
    {
        SysFileInfo sysFileInfo = sysFileInfoService.selectSysFileInfoById(fileId);
        mmap.put("sysFileInfo", sysFileInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件信息
     */
    @RequiresPermissions("company:uploadfile:edit")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysFileInfo sysFileInfo)
    {
        return toAjax(sysFileInfoService.updateSysFileInfo(sysFileInfo));
    }

    /**
     * 删除文件信息
     */
    @RequiresPermissions("company:uploadfile:remove")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoByIds(ids);
        for(int i = 0; i<list.size(); i++){
            String newfilePath = list.get(i).getFilePath().replace("/profile/upload",filePath);
            File file = new File(newfilePath);
            boolean isOk = file.exists();
            if(isOk){
                file.delete();
                logger.info(list.get(i).getFileId()+"+删除成功！");
            }else{
                logger.info(list.get(i).getFileId()+"+文件不存在！");
            }
        }
        return toAjax(sysFileInfoService.deleteSysFileInfoByIds(ids));
    }
}
